<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="课程" name="courseId">
						<a-select
							v-model:value="searchFormState.courseId"
							show-search
							:options="courseData"
							:filter-option="filterOption"
							:field-names="{ label: 'name', value: 'id' }"
							style="width: 100%"
							placeholder="请选择课程"
							allow-clear
						>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="班级" name="klassId">
						<a-select
							v-model:value="searchFormState.klassId"
							show-search
							:options="klassData"
							:filter-option="filterOption"
							:field-names="{ label: 'name', value: 'id' }"
							style="width: 100%"
							placeholder="请选择班级"
							allow-clear
						>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="状态" name="state">
						<a-select
							v-model:value="searchFormState.state"
							:options="stateOptions"
							style="width: 100%"
							placeholder="请选择"
							allow-clear
						>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="table.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('eduTutorialAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<a-button @click="createLecture">
						<template #icon><export-outlined /></template>
						小班开课
					</a-button>
					<a-button @click="combineClass">
						<template #icon><export-outlined /></template>
						{{ $t('edu.combineClass') }}
					</a-button>
					<xn-batch-delete
						v-if="hasPerm('eduTutorialBatchDelete')"
						:selectedRowKeys="selectedRowKeys"
						@batchDelete="deleteBatchEduTutorial"
					/>
					<a-button @click="exportBatchTutorialVerify">
						<template #icon><export-outlined /></template>
						导出课表
					</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'state'">
					{{ record.state == 1?'已开课':'未开课' }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('eduTutorialEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['eduTutorialEdit', 'eduTutorialDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteEduTutorial(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('eduTutorialDelete')">删除</a-button>
						</a-popconfirm>
						<a-divider type="vertical" v-if="hasPerm(['eduTutorialEdit', 'eduTutorialDelete'], 'and')" />
						<a-dropdown v-if="hasPerm(['eduTutorialEdit', 'eduTutorialDelete'], 'and')">
							<a class="ant-dropdown-link">
								{{ $t('common.more') }}
								<DownOutlined />
							</a>
							<template #overlay>
								<a-menu>
									<a-menu-item v-if="hasPerm('eduTutorialEdit')">
										<a @click="lectureRef.onOpen(null,record)" v-if="record.state != 1 && hasPerm('eduTutorialEdit')">开课</a>
									</a-menu-item>
								</a-menu>
							</template>
						</a-dropdown>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="table.refresh(true)" />
	<Lecture ref="lectureRef" @successful="table.refresh(true)" />
	<BatchEdit ref="batchEditRef" @successful="table.refresh(true)" />
</template>

<script setup name="tutorial">
	import Form from './form.vue'
	import Lecture from '../lecture/form.vue'
	import BatchEdit from './batchEdit.vue'
	import eduTutorialApi from '@/api/biz/eduTutorialApi'
	import eduLectureApi from '@/api/biz/eduLectureApi'
	import {message} from "ant-design-vue";
	import userApi from "@/api/sys/userApi";
	import downloadUtil from "@/utils/downloadUtil";
	import tool from '@/utils/tool'

	let searchFormState = reactive({})
	const searchFormRef = ref()
	const table = ref()
	const formRef = ref()
	const lectureRef = ref()
	const batchEditRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }

	// 教师数据
	let teacherData = ref([])

	//课程数据
	let courseData = ref([])

	//班级数据
	let klassData = ref([])

	const columns = [
		{
			title: '课程',
			dataIndex: 'courseName'
		},
		{
			title: '总学时',
			dataIndex: 'classHour'
		},
		{
			title: '学期学时数',
			dataIndex: 'classHour'
		},
		{
			title: '学分',
			dataIndex: 'credit'
		},
		{
			title: '班级',
			dataIndex: 'klassName'
		},
		{
			title: '人数',
			dataIndex: 'size'
		},
		{
			title: '状态',
			dataIndex: 'state'
		},
		{
			title: '备注',
			dataIndex: 'remarks'
		}
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['eduTutorialEdit', 'eduTutorialDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: '230px'
		})
	}
	const stateOptions = tool.dictList('TUTORIAL_STATE')
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
    const filterOption = (input, option) => {
		return option.name.toLowerCase().indexOf(input.toLowerCase()) >= 0;
    }
	const loadData = (parameter) => {
		seleTeacherData()
		seleCourseData()
		seleKlassData()
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		return eduTutorialApi.eduTutorialPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 选择教师
	const seleTeacherData = () => {
		const param = {}
		eduTutorialApi.eduTeacherList(param).then((res) => {
			teacherData.value = res
		})
	}
	// 选择课程
	const seleCourseData = () => {
		const param = {}
		eduTutorialApi.eduCourseList(param).then((res) => {
			courseData.value = res
		})
	}
	// 选择班级
	const seleKlassData = () => {
		const param = {}
		eduTutorialApi.eduKlassList(param).then((res) => {
			klassData.value = res
		})
	}
	// 批量修改
	const batchEdit = () => {
		if (selectedRowKeys.value.length < 1) {
			message.warning('请勾选要修改的记录')
			return
		}
		const params = {
			tutorialIds: selectedRowKeys.value
				.map((m) => {
					return m
				})
				.join()
		}
		batchEditRef.value.onOpen(params)
	}
	// 小班开课
	const createLecture = () => {
		if (selectedRowKeys.value.length < 1) {
			message.warning('请勾选要开课的记录')
			return
		}
		if (selectedRowKeys.value.length > 0) {
			const params = {
				tutorialIds: selectedRowKeys.value
					.map((m) => {
						return m
					})
					.join()
			}
			console.log(params)
			eduLectureApi.eduLectureCreate(params).then(() => {
				message.success('操作成功')
				table.value.clearSelected()
				table.value.refresh(true)
			})
		}
	}
	//合班开课
	const combineClass = () => {
		if (selectedRowKeys.value.length < 2) {
			message.warning('请勾选要2个以上合班的信息')
			return
		}
		if (selectedRowKeys.value.length > 0) {
			const params = {
				tutorialIds: selectedRowKeys.value
					.map((m) => {
						return m
					})
					.join()
			}
			console.log(params)
			eduLectureApi.eduLectureMerge(params).then(() => {
				message.success('操作成功')
				table.value.clearSelected()
				table.value.refresh(true)
			})
			return
		}
	}
	// 批量导出校验并加参数
	const exportBatchTutorialVerify = () => {
		if ((selectedRowKeys.value.length < 1) & !searchFormState.klassId) {
			message.warning('请输入查询条件或勾选要导出的信息')
		}
		if (selectedRowKeys.value.length > 0) {
			const params = {
				klassId: searchFormState.klassId,
				year: searchFormState.year,
				semester: searchFormState.semester,
				tutorialIds: selectedRowKeys.value
					.map((m) => {
						return m
					})
					.join()
			}
			exportBatchTutorial(params)
			return
		}
	}
	// 批量导出
	const exportBatchTutorial = (params) => {
		console.log(params)
		eduTutorialApi.tutorialExport(params).then((res) => {
			downloadUtil.resultDownload(res)
			table.value.clearSelected()
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		table.value.refresh(true)
	}
	// 删除
	const deleteEduTutorial = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		eduTutorialApi.eduTutorialDelete(params).then(() => {
			table.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchEduTutorial = (params) => {
		eduTutorialApi.eduTutorialDelete(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}
</script>
